<?php
/**
 * Created by PhpStorm.
 * User: DY
 * Date: 2017/3/10
 * Time: 15:58
 */
namespace App\Http\Controllers;
require_once ('../app/Http/SDK/mns-autoloader.php');

use App\HTTP\Controllers\Controller;
use Mts\Request\V20140618 as Mts;
use Sts\Request\V20150401 as Sts;
use OSS\OssClient;
use App\Http\Common\OSSConfig;
use App\Common\Common;


use AliyunMNS\Client;
use AliyunMNS\Topic;
use AliyunMNS\Constants;
use AliyunMNS\Model\MailAttributes;
use AliyunMNS\Model\SmsAttributes;
use AliyunMNS\Model\BatchSmsAttributes;
use AliyunMNS\Model\MessageAttributes;
use AliyunMNS\Exception\MnsException;
use AliyunMNS\Requests\PublishMessageRequest;

require_once "SignatureHelper.php";

use Aliyun\DySDKLite\SignatureHelper;


class OSSController extends Controller
{

    var $ossClient = null;
    var $client = null;
    var $accessKeyID = null; // 替换成真实的id
    var $accessKeySecret = null; // 替换成真实的secret
    var $endpoint_hz = 'cn-hangzhou';
    var $endpoint_bj = 'cn-beijing';
    var $smsurl = 'http://sms.aliyuncs.com?';




    /**获取临时授权DY
     * @return array
     * @throws \ClientException
     */



    public function run($mobile,$code)
    {
        /**
         * Step 1. 初始化Client
         */
        $this->endPoint = "http://1977680567921435.mns.cn-hangzhou.aliyuncs.com/"; // eg. http://1234567890123456.mns.cn-shenzhen.aliyuncs.com
        $this->accessId = "LTAIZwZEnzqRP0nP";
        $this->accessKey = "IdtkdE8F8S8xKBZMScvliW3mCWbAGR";

        $this->client = new Client($this->endPoint, $this->accessId, $this->accessKey);
        /**
         * Step 2. 获取主题引用
         */
        $topicName = "sms.topic-cn-hangzhou";
        $topic = $this->client->getTopicRef($topicName);
        /**
         * Step 3. 生成SMS消息属性
         */
        // 3.1 设置发送短信的签名（SMSSignName）和模板（SMSTemplateCode）
        $batchSmsAttributes = new BatchSmsAttributes("牛魔科技", "SMS_70265262");
        // 3.2 （如果在短信模板中定义了参数）指定短信模板中对应参数的值
        $batchSmsAttributes->addReceiver("$mobile", array("code" => "$code","product"=>"牛魔科技"));
        $messageAttributes = new MessageAttributes(array($batchSmsAttributes));
        /**
         * Step 4. 设置SMS消息体（必须）
         *
         * 注：目前暂时不支持消息内容为空，需要指定消息内容，不为空即可。
         */
        $messageBody = "smsmessage";
        /**
         * Step 5. 发布SMS消息
         */
        $request = new PublishMessageRequest($messageBody, $messageAttributes);
        try
        {
            $res = $topic->publishMessage($request);
            return $res;
        }
        catch (MnsException $e)
        {
            return $e;
        }
    }



    function sendSms($mobile, $verify_code) {

        $params = array ();

        // *** 需用户填写部分 ***

        // fixme 必填: 请参阅 https://ak-console.aliyun.com/ 取得您的AK信息
        $accessKeyId = "LTAIiisPZrbkGAwn";
        $accessKeySecret = "v0C3EZD0zAFjnrmQcjd2cebkcNIiXi";

        // fixme 必填: 短信接收号码
        $params["PhoneNumbers"] = $mobile;

        // fixme 必填: 短信签名，应严格按"签名名称"填写，请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/sign
//    $params["SignName"] = "阿里云短信测试专用";
        $params["SignName"] = "北京牛魔科技有限公司";

        // fixme 必填: 短信模板Code，应严格按"模板CODE"填写, 请参考: https://dysms.console.aliyun.com/dysms.htm#/develop/template
        $params["TemplateCode"] = "SMS_89890262";

        // fixme 可选: 设置模板参数, 假如模板中存在变量需要替换则为必填项
        $params['TemplateParam'] = Array (
            "code" => $verify_code,
            "product" => "牛魔科技"
        );

        // fixme 可选: 设置发送短信流水号
        $params['OutId'] = "12345";

        // fixme 可选: 上行短信扩展码, 扩展码字段控制在7位或以下，无特殊需求用户请忽略此字段
        $params['SmsUpExtendCode'] = "1234567";


        // *** 需用户填写部分结束, 以下代码若无必要无需更改 ***
        if(!empty($params["TemplateParam"]) && is_array($params["TemplateParam"])) {
            $params["TemplateParam"] = json_encode($params["TemplateParam"], JSON_UNESCAPED_UNICODE);
        }

        // 初始化SignatureHelper实例用于设置参数，签名以及发送请求
        $helper = new SignatureHelper();

//var_dump($accessKeyId,$accessKeySecret,$params);die;
        // 此处可能会抛出异常，注意catch
        $content = $helper->request(
            $accessKeyId,
            $accessKeySecret,
            "dysmsapi.aliyuncs.com",
            array_merge($params, array(
                "RegionId" => "cn-hangzhou",
                "Action" => "SendSms",
                "Version" => "2017-05-25",
            ))
        );

        return $content;
    }


}

?>